package eu.vranckaert.workRegistrationWidget.activities;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.location.Location;
import android.os.Bundle;
import com.google.inject.Inject;
import eu.vranckaert.workRegistrationWidget.R;
import eu.vranckaert.workRegistrationWidget.domain.TimeRegistration;
import eu.vranckaert.workRegistrationWidget.exceptions.EndDateBeforeStartDateException;
import eu.vranckaert.workRegistrationWidget.service.LocationService;
import eu.vranckaert.workRegistrationWidget.service.RegistrationService;
import eu.vranckaert.workRegistrationWidget.service.WidgetLayoutBuilderService;
import eu.vranckaert.workRegistrationWidget.util.Preferences;
import eu.vranckaert.workRegistrationWidget.util.WidgetUpdater;
import roboguice.activity.GuiceActivity;

/**
 * @author Dirk Vranckaert
 * Date: 21-apr-2010
 * Time: 22:12:16
 */
public class RegisterWorkActivity extends GuiceActivity {

    private static final String LOG_TAG = RegisterWorkActivity.class.getName();

    private static final int DIALOG_EXCEPTION_END_DATE_START_DATE = 900;

    @Inject
    RegistrationService registrationService;

    @Inject
    WidgetLayoutBuilderService widgetLayoutBuilderService;

    @Inject
    LocationService locationService;

    @Override
    protected Dialog onCreateDialog(int id) {
        Dialog dialog = null;

        switch (id) {
            case DIALOG_EXCEPTION_END_DATE_START_DATE:
            AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
            alertBuilder.setTitle(R.string.warning)
                       .setMessage(R.string.widget_exc_enddate_before_startdate)
                       .setCancelable(false)
                       .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                                finish();
                            }
                        });
            AlertDialog alertDialog = alertBuilder.create();
            dialog = alertDialog;
            break;
        }

        return dialog;
    }

	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

        boolean showingDialog = false;

        TimeRegistration registration = null;
        try {
            registration = registrationService.createRegistrationForCurrentTime(this);
            WidgetUpdater.updateWidget(this, registration, widgetLayoutBuilderService);
        } catch (EndDateBeforeStartDateException e) {
            showDialog(DIALOG_EXCEPTION_END_DATE_START_DATE);
            showingDialog = true;
        }

        boolean gpsUsageAllowed = Preferences.getGpsUsageAllowed(this);
        if (!showingDialog && gpsUsageAllowed) {
            locationService.determineLocation(this, registration);
        }

        if (!showingDialog) {
		    super.finish();
        }
	}
}
